package com.ruoyi.ebike.service.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.ebike.mapper.VehicleMapper;
import com.ruoyi.ebike.domain.Vehicle;
import com.ruoyi.ebike.service.IVehicleService;

/**
 * 车辆列表Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-12-18
 */
@Service
public class VehicleServiceImpl implements IVehicleService 
{
    @Autowired
    private VehicleMapper vehicleMapper;

    /**
     * 查询车辆列表
     * 
     * @param id 车辆列表主键
     * @return 车辆列表
     */
    @Override
//    @DataScope(deptAlias = "v")
    public Vehicle selectVehicleById(Long id)
    {
        Vehicle vehicle = vehicleMapper.selectVehicleById(id);
        return vehicle;
    }

    /**
     * 查询车辆列表列表
     * 
     * @param vehicle 车辆列表
     * @return 车辆列表
     */
    @Override
    @DataScope(deptAlias = "v")
    public List<Vehicle> selectVehicleList(Vehicle vehicle)
    {
        List<Vehicle> vehicleList = vehicleMapper.selectVehicleList(vehicle);
        return vehicleList;
    }


    /**
     * 修改车辆列表
     * 
     * @param vehicle 车辆列表
     * @return 结果
     */
    @Override
    public int updateVehicle(Vehicle vehicle)
    {
        return vehicleMapper.updateVehicle(vehicle);
    }

    /**
     * 批量删除车辆列表
     * 
     * @param ids 需要删除的车辆列表主键
     * @return 结果
     */
    @Override
    public int deleteVehicleByIds(Long[] ids)
    {
        return vehicleMapper.deleteVehicleByIds(ids);
    }

    /**
     * 删除车辆列表信息
     * 
     * @param id 车辆列表主键
     * @return 结果
     */
    @Override
    public int deleteVehicleById(Long id)
    {
        return vehicleMapper.deleteVehicleById(id);
    }

    /**
     * 车辆运营看板
     */
    public Map<String, Long> getVehicleStatusCount() {
        List<Vehicle> vehicles = vehicleMapper.selectAllVehicles();
        Map<String, Long> statusCount = new HashMap<>();
        statusCount.put("空闲", 0L);
        statusCount.put("骑行中车辆", 0L);
        statusCount.put("故障车辆", 0L);
        statusCount.put("运维占用", 0L);
        for (Vehicle vehicle : vehicles) {
            Long operationStatus = vehicle.getOperationStatus();
            if (operationStatus != null) {
                switch (operationStatus.intValue()) {
                    case 0:
                        statusCount.put("空闲", statusCount.get("空闲") + 1);
                        break;
                    case 2:
                        statusCount.put("骑行中车辆", statusCount.get("骑行中车辆") + 1);
                        break;
                    case 3:
                        statusCount.put("故障车辆", statusCount.get("故障车辆") + 1);
                        break;
                    case 4:
                        statusCount.put("运维占用", statusCount.get("运维占用") + 1);
                        break;
                }
            }
        }
        return statusCount;
    }


    /**
     * 统计车辆区域
     */

    public BigDecimal getAreaCountByDeptId(Long deptId) {
        return vehicleMapper.getAreaCountByDeptId(deptId);
    }

    public BigDecimal getAreaIncomeByDeptId(Long deptId) {
        return vehicleMapper.getAreaIncomeByDeptId(deptId);
    }
}



